package controllers

import (
	"github.com/astaxie/beego"
	"net/url"
	"nspel/entity"
	"nspel/model"
	"nspel/nspel"
	"nspel/util"
	"strings"
)

type ReportManageController struct {
	beego.Controller
}

func (c *ReportManageController) GetReportPackageList() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	if authority != entity.ManagerAuthority.Area && authority != entity.ManagerAuthority.Administrator {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "不支持请求"}
		return
	}
	res := model.GetReportPackageListRes{RetCode: 0}
	var count int64
	var schools []entity.School
	var err error
	if authority == entity.ManagerAuthority.Administrator {
		req := model.GetReportPackageListAdministrator{}
		b := req.Validate("province-id", "city-id", "area-id", "school-name", "join-year", "page-size", "current-page", &c.Controller)
		if !b {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		count, schools, err = util.GetSchoolsListAdministrator(req.ProvinceId, req.CityId, req.AreaId, req.SchoolName, req.JoinYear, req.PageSize, req.CurrentPage)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Total = count
	} else if authority == entity.ManagerAuthority.Area {
		req := model.GetReportPackageListArea{}
		b, err := req.Validate("join-year", "school-id", "page-size", "current-page", &c.Controller, uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		if !b {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		count, schools, err = util.GetSchoolsListArea(area.Id, req.JoinYear, req.SchoolId, req.PageSize, req.CurrentPage)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Total = count
	} else {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "不支持请求"}
		return
	}
	res.Data = make([]model.ReportPackageList, len(schools))
	for i := 0; i < len(res.Data); i++ {
		province := new(entity.Province)
		err = province.SelectAllById(schools[i].ProvinceId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		city := new(entity.City)
		err = city.SelectAllById(schools[i].CityId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		area := new(entity.Area)
		err = area.SelectAllById(schools[i].ExperimentAreaId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Data[i].Id = schools[i].Id
		res.Data[i].Name = schools[i].Name
		res.Data[i].ProvinceCityArea = province.Name + city.Name + area.Name
	}
	c.Data["json"] = res
}

func (c *ReportManageController) GetReportSchoolExist() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	var schoolId int64
	var err error
	school := new(entity.School)
	var pdf bool
	if authority == entity.ManagerAuthority.Administrator {
		schoolId, err = c.GetInt64("id")
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		err = school.SelectAllById(schoolId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		pdf, err = c.GetBool("pdf")
		if err != nil {
			pdf = false
		}
	} else if authority == entity.ManagerAuthority.Area {
		pdf = true
		schoolId, err = c.GetInt64("id")
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		err = school.SelectAllById(schoolId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		if school.ExperimentAreaId != area.Id {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
	} else if authority == entity.ManagerAuthority.School {
		pdf = true
		err = school.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		schoolId = school.Id
	}
	province := new(entity.Province)
	err = province.SelectAllById(school.ProvinceId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	city := new(entity.City)
	err = city.SelectAllById(school.CityId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	area := new(entity.Area)
	err = area.SelectAllById(school.ExperimentAreaId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + "/" + school.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + "/" + school.Name + ".zip"
	}
	ok, err = util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "文件不存在"}
	} else {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
	}
}

func (c *ReportManageController) GetReportSchool() {
	c.EnableRender = false
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Ctx.ResponseWriter.Status = 401
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Ctx.ResponseWriter.Status = 401
		return
	}
	var schoolId int64
	var err error
	var pdf bool
	school := new(entity.School)
	if authority == entity.ManagerAuthority.Administrator {
		schoolId, err = c.GetInt64("id")
		if err != nil {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
		err = school.SelectAllById(schoolId)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		pdf, err = c.GetBool("pdf")
		if err != nil {
			pdf = false
		}
	} else if authority == entity.ManagerAuthority.Area {
		pdf = true
		schoolId, err = c.GetInt64("id")
		if err != nil {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		err = school.SelectAllById(schoolId)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		if school.ExperimentAreaId != area.Id {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
	} else if authority == entity.ManagerAuthority.School {
		pdf = true
		err = school.SelectAllByUserId(uid)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		schoolId = school.Id
	}
	province := new(entity.Province)
	err = province.SelectAllById(school.ProvinceId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	city := new(entity.City)
	err = city.SelectAllById(school.CityId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	area := new(entity.Area)
	err = area.SelectAllById(school.ExperimentAreaId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + "/" + school.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + "/" + school.Name + ".zip"
	}
	ok, err = util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Ctx.ResponseWriter.Status = 404
		return
	} else {
		c.Ctx.Output.Header("X-Accel-Redirect", path[1:])
		c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(school.Name+".zip"))
		c.Ctx.Output.Header("Content-Description", "File Transfer")
		c.Ctx.Output.Header("Content-Type", "application/octet-stream")
		c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
		c.Ctx.Output.Header("Expires", "0")
		c.Ctx.Output.Header("Cache-Control", "must-revalidate")
		c.Ctx.Output.Header("Pragma", "public")
	}
}

func (c *ReportManageController) GetReportAreaExist() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	var err error
	var pdf bool
	area := new(entity.Area)
	if authority == entity.ManagerAuthority.Administrator {
		var areaId int64
		areaId, err = c.GetInt64("id")
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		err = area.SelectAllById(areaId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		pdf, err = c.GetBool("pdf")
		if err != nil {
			pdf = false
		}
	} else if authority == entity.ManagerAuthority.Area {
		pdf = true
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	}
	city := new(entity.City)
	err = city.SelectAllById(area.CityId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	province := new(entity.Province)
	err = province.SelectAllById(area.ProvinceId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + ".zip"
	}
	ok, err = util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "文件不存在"}
	} else {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
	}
}

func (c *ReportManageController) GetReportArea() {
	c.EnableRender = false
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Ctx.ResponseWriter.Status = 401
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Ctx.ResponseWriter.Status = 401
		return
	}
	var pdf bool
	var err error
	area := new(entity.Area)
	if authority == entity.ManagerAuthority.Administrator {
		var areaId int64
		areaId, err = c.GetInt64("id")
		if err != nil {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
		err = area.SelectAllById(areaId)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		pdf, err = c.GetBool("pdf")
		if err != nil {
			pdf = false
		}
	} else if authority == entity.ManagerAuthority.Area {
		pdf = true
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	}
	city := new(entity.City)
	err = city.SelectAllById(area.CityId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	province := new(entity.Province)
	err = province.SelectAllById(area.ProvinceId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "/" + area.Name + ".zip"
	}
	ok, err = util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Ctx.ResponseWriter.Status = 404
		return
	} else {
		c.Ctx.Output.Header("X-Accel-Redirect", path[1:])
		c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(area.Name+".zip"))
		c.Ctx.Output.Header("Content-Description", "File Transfer")
		c.Ctx.Output.Header("Content-Type", "application/octet-stream")
		c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
		c.Ctx.Output.Header("Expires", "0")
		c.Ctx.Output.Header("Cache-Control", "must-revalidate")
		c.Ctx.Output.Header("Pragma", "public")
	}
}

func (c *ReportManageController) GetReportCityExist() {
	defer c.ServeJSON()
	cityId, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	city := new(entity.City)
	err = city.SelectAllById(cityId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	province := new(entity.Province)
	err = province.SelectAllById(city.ProvinceId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	var pdf bool
	pdf, err = c.GetBool("pdf")
	if err != nil {
		pdf = false
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + "/" + city.Name + ".zip"
	}
	ok, err := util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "文件不存在"}
	} else {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
	}
}

func (c *ReportManageController) GetReportCity() {
	c.EnableRender = false
	cityId, err := c.GetInt64("id")
	if err != nil {
		c.Ctx.ResponseWriter.Status = 400
		return
	}
	city := new(entity.City)
	err = city.SelectAllById(cityId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	province := new(entity.Province)
	err = province.SelectAllById(city.ProvinceId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	var pdf bool
	pdf, err = c.GetBool("pdf")
	if err != nil {
		pdf = false
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "/" + city.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + "/" + city.Name + ".zip"
	}
	ok, err := util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Ctx.ResponseWriter.Status = 404
	} else {
		c.Ctx.Output.Header("X-Accel-Redirect", path[1:])
		c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(city.Name+".zip"))
		c.Ctx.Output.Header("Content-Description", "File Transfer")
		c.Ctx.Output.Header("Content-Type", "application/octet-stream")
		c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
		c.Ctx.Output.Header("Expires", "0")
		c.Ctx.Output.Header("Cache-Control", "must-revalidate")
		c.Ctx.Output.Header("Pragma", "public")
	}
}

func (c *ReportManageController) GetReportProvinceExist() {
	defer c.ServeJSON()
	provinceId, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	province := new(entity.Province)
	err = province.SelectAllById(provinceId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	var pdf bool
	pdf, err = c.GetBool("pdf")
	if err != nil {
		pdf = false
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + ".zip"
	}
	ok, err := util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "文件不存在"}
	} else {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
	}
}

func (c *ReportManageController) GetReportProvince() {
	c.EnableRender = false
	provinceId, err := c.GetInt64("id")
	if err != nil {
		c.Ctx.ResponseWriter.Status = 400
		return
	}
	province := new(entity.Province)
	err = province.SelectAllById(provinceId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	var pdf bool
	pdf, err = c.GetBool("pdf")
	if err != nil {
		pdf = false
	}
	var path string
	if pdf {
		path = "./reportFiles/" + province.Name + "pdf.zip"
	} else {
		path = "./reportFiles/" + province.Name + ".zip"
	}
	ok, err := util.PathExists(path)
	if err != nil {
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	}
	if !ok {
		c.Ctx.ResponseWriter.Status = 404
		return
	} else {
		c.Ctx.Output.Header("X-Accel-Redirect", path[1:])
		c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(province.Name+".zip"))
		c.Ctx.Output.Header("Content-Description", "File Transfer")
		c.Ctx.Output.Header("Content-Type", "application/octet-stream")
		c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
		c.Ctx.Output.Header("Expires", "0")
		c.Ctx.Output.Header("Cache-Control", "must-revalidate")
		c.Ctx.Output.Header("Pragma", "public")
	}
}

func (c *ReportManageController) GetReportSubList() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	res := model.GetReportSubListRes{RetCode: 0}
	var count int64
	var reports []entity.Report
	var err error
	if authority == entity.ManagerAuthority.Administrator {
		req := model.GetReportPackageListAdministrator{}
		b := req.Validate("province-id", "city-id", "area-id", "school-name", "join-year", "page-size", "current-page", &c.Controller)
		if !b {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		count, reports, err = util.GetReportsListAdministrator(req.ProvinceId, req.CityId, req.AreaId, req.SchoolName, req.JoinYear, req.PageSize, req.CurrentPage)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Total = count
	} else if authority == entity.ManagerAuthority.Area {
		req := model.GetReportPackageListArea{}
		b, err := req.Validate("join-year", "school-id", "page-size", "current-page", &c.Controller, uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		if !b {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		count, reports, err = util.GetReportsListArea(area.Id, req.JoinYear, req.SchoolId, req.PageSize, req.CurrentPage)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Total = count
	} else {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "不支持请求"}
		return
	}
	res.Total = count
	res.Data = make([]model.ReportSubList, len(reports))
	for i := 0; i < len(reports); i++ {
		res.Data[i].Id = reports[i].Id
		province := new(entity.Province)
		err = province.SelectAllById(reports[i].ProvinceId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		city := new(entity.City)
		err = city.SelectAllById(reports[i].CityId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		area := new(entity.Area)
		err = area.SelectAllById(reports[i].ExperimentAreaId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Data[i].ProvinceCityArea = province.Name + city.Name + area.Name
		res.Data[i].IsUpdate = util.GetBoolString(reports[i].IsUpdate, "是", "否")
		res.Data[i].Info = reports[i].Info.String
		res.Data[i].SchoolName = reports[i].Tip.String
		res.Data[i].MeasureDate = reports[i].MeasureTime.Format("2006年01月02日")
		if reports[i].Type == entity.SchoolTypeReport {
			res.Data[i].Type = "学校报告"
		} else if reports[i].Type == entity.GradeTypeReport {
			res.Data[i].Type = "年级报告"
		} else if reports[i].Type == entity.ClassTypeReport {
			res.Data[i].Type = "班级报告"
		}
	}
	c.Data["json"] = res
}

func (c *ReportManageController) GetReportSubExist() {
	defer c.ServeJSON()
	var pdf bool
	reportId, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	report := new(entity.Report)
	err = report.SelectAllById(reportId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if authority == entity.ManagerAuthority.Area {
		pdf = true
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		if area.Id != report.ExperimentAreaId {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
	} else {
		pdf, err = c.GetBool("pdf")
		if err != nil {
			pdf = false
		}
	}
	if !report.Url.Valid {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "文件不存在"}
		return
	}
	var path string
	if pdf {
		path = report.Url.String[:len(report.Url.String)-4] + "pdf"
	} else {
		path = report.Url.String
	}
	b, err := util.PathExists(path)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if !b {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "文件不存在"}
	} else {
		c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
	}
}

func (c *ReportManageController) GetReportSub() {
	defer c.ServeJSON()
	var pdf bool
	reportId, err := c.GetInt64("id")
	if err != nil {
		c.Ctx.ResponseWriter.Status = 400
		return
	}
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Ctx.ResponseWriter.Status = 401
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Ctx.ResponseWriter.Status = 401
		return
	}
	report := new(entity.Report)
	err = report.SelectAllById(reportId)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	if authority == entity.ManagerAuthority.Area {
		pdf = true
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		if area.Id != report.ExperimentAreaId {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
	} else {
		pdf, err = c.GetBool("pdf")
		if err != nil {
			pdf = false
		}
	}
	if !report.Url.Valid {
		c.Ctx.ResponseWriter.Status = 404
		return
	}
	var path string
	if pdf {
		path = report.Url.String[:len(report.Url.String)-4] + "pdf"
	} else {
		path = report.Url.String
	}
	b, err := util.PathExists(path)
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	if !b {
		c.Ctx.ResponseWriter.Status = 404
	} else {
		c.Ctx.Output.Header("X-Accel-Redirect", path[1:])
		c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(report.Url.String[strings.LastIndex(report.Url.String, "/")+1:]))
		c.Ctx.Output.Header("Content-Description", "File Transfer")
		c.Ctx.Output.Header("Content-Type", "application/octet-stream")
		c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
		c.Ctx.Output.Header("Expires", "0")
		c.Ctx.Output.Header("Cache-Control", "must-revalidate")
		c.Ctx.Output.Header("Pragma", "public")
	}
}

func (c *ReportManageController) PostReportSubMarkForRebuild() {
	defer c.ServeJSON()
	reportId, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	report := new(entity.Report)
	err = report.SelectAllById(reportId)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if authority == entity.ManagerAuthority.Area {
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		if area.Id != report.ExperimentAreaId {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
	}
	report.IsUpdate = true
	err = report.Update("is_update")
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if report.Type == entity.ClassTypeReport {
		score := new(entity.Score)
		err = nspel.GetOrmEngine().QueryTable(score.TableName()).Filter("measurement_date", report.MeasureTime.Format("2006-01-02")).Filter("class", report.Info.String).One(score)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		report.Info = util.GetNullString(score.Grade)
		report.Type = entity.GradeTypeReport
		err = nspel.GetOrmEngine().Read(report, "info", "measure_time", "type")
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		report.IsUpdate = true
		err = report.Update("is_update")
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	}
	c.Data["json"] = map[string]interface{}{"retCode": 0, "msg": "更新成功"}
	defer nspel.RedisKeyDelete("report")
}
